MODULE sources

USE definitions

IMPLICIT NONE

CONTAINS

SUBROUTINE wind_surce

  USE definitions
  
  IMPLICIT NONE
  
  REAL, DIMENSION(:), ALLOCATABLE :: flow
  INTEGER :: i, trigger

  IF(sourceeq == 1) THEN
     sourceflow = sourcebuf/REAL(ysize)
  ENDIF
  
  ALLOCATE(flow(1:ysize))
  DO i=1,ysize
     IF(grads == 2) flow(i) = 1.9*ncov*(E0 + (E1-E0)/REAL(ysize)*i)*&
          (L0 + (L1-L0)/REAL(ysize)*i)
     IF(gradS == 1) flow(i) = sourceflow + (sourceflow1-sourceflow)/REAL(ysize)*i
     IF(gradS == 0) flow(i) = sourceflow
  ENDDO
  
 CALL wind_source_in(flow, 1)

END SUBROUTINE wind_surce

SUBROUTINE wind_source_in(flow, side)

  USE definitions
  USE transport
  USE deposition

  IMPLICIT NONE

  INTEGER, INTENT(in) :: side
  REAL, INTENT(in), DIMENSION(:) :: flow
  INTEGER :: i, j, x, y, trigx, trigy
  INTEGER, DIMENSION(2) :: pos, npos
  REAL :: rand, L




  SELECT CASE(side)
     CASE(1)
        DO i = 1,ysize
       npos (1) = 1
           CALL random_number(rand)
           npos(2) = INT(rand*(ysize-1))+1
           CALL calcL(npos, L)
           DO
              trigx = 0
              trigy = 0
              CALL find_next_cell(npos, trigx, trigy, L, flow(npos(2)))
              IF(trigx == 1 .OR. trigy == 1) EXIT
              IF(depose(npos,flow(npos(2)), L)==1) EXIT
           ENDDO
        ENDDO
     CASE(2)
        DO i = 1,xsize
           npos (2) = ysize
           CALL random_number(rand)
           npos(1) = INT(rand*(xsize-1))+1
           CALL calcL(npos, L)
           DO
              trigx = 0
              trigy = 0
              CALL find_next_cell(npos, trigx, trigy, L, flow(npos(1)))
              IF(trigx == 1 .OR. trigy == 1) EXIT
              IF(depose(npos,flow(npos(1)), L)==1) EXIT
           ENDDO
        ENDDO
     CASE(3)
        DO i = 1,ysize
           npos (1) = xsize
           CALL random_number(rand)
           npos(2) = INT(rand*(ysize-1))+1
           CALL calcL(npos, L)
           DO
              trigx = 0
              trigy = 0
              CALL find_next_cell(npos, trigx, trigy, L, flow(npos(2)))
              IF(trigx == 1 .OR. trigy == 1) EXIT
              IF(depose(npos,flow(npos(2)), L)==1) EXIT
           ENDDO
        ENDDO
     CASE(4)
        DO i = 1,xsize
           npos (2) = 1
           CALL random_number(rand)
           npos(1) = INT(rand*(xsize-1))
           CALL calcL(npos, L)
           DO
              trigx = 0
              trigy = 0
              CALL find_next_cell(npos, trigx, trigy, L, flow(npos(1)))
              IF(trigx == 1 .OR. trigy == 1) EXIT
               IF(depose(npos,flow(npos(1)), L)==1) EXIT
           ENDDO
        ENDDO
   END SELECT

           

END SUBROUTINE wind_source_in

END MODULE sources
